package com.zd.bms.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.bms.bean.vo.req.BmsRoleReqListVo;
import com.zd.bms.bean.vo.req.RoleMenuTreeVoReqVo;
import com.zd.bms.bean.vo.resp.BmsRoleRespVo;
import com.zd.bms.service.IBmsRoleService;
import com.zd.common.bean.model.BmsResource;
import com.zd.common.bean.model.BmsRole;
import com.zd.common.common.utils.LogUtils;
import com.zd.common.dao.common.Page;

@Controller
@RequestMapping("/role")
public class BmsRoleController extends BaseController{
	
	@Autowired
	private IBmsRoleService bmsRoleService;
	
	@RequestMapping("/index")
	public ModelAndView index()throws Exception{
		ModelAndView mv = new ModelAndView();
		mv.setViewName("role/index");
		return mv;
	}
	
	@RequestMapping("/resourceIndex")
	public ModelAndView resourceIndex()throws Exception{
		ModelAndView mv = new ModelAndView();
		mv.setViewName("role/resourceIndex");
		return mv;
	}
	
	@RequestMapping("/resourceList")
	@ResponseBody
	public Map<String, Object> resourceList()throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("resourceRespVoList", bmsRoleService.findAllResource());
		return resultMap;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(BmsRoleReqListVo reqVo)throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		Page<BmsRoleRespVo> bmsRoleRespVoPage = bmsRoleService.findAllRole(reqVo);
		resultMap.put("data", bmsRoleRespVoPage.getList());
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", bmsRoleRespVoPage.getList().size());
		resultMap.put("recordsFiltered", bmsRoleRespVoPage.getTotalRow());

		return resultMap;
	}
	
	@RequestMapping("/saveOrUpdateIndex")
	public ModelAndView toSaveOrUpdateIndex(Integer roleId) throws Exception{
		ModelAndView mv = new ModelAndView();
		if(roleId != null){
			BmsRole bmsRole = bmsRoleService.get(roleId);
			mv.addObject("bmsRole", bmsRole);
		}
		mv.setViewName("role/saveOrUpdateIndex");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/menuTreeList")
	public Map<String, Object> menuTreeList(Integer roleId) throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		resultMap = bmsRoleService.findRole(roleId);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("/saveOrUpdateRole")
	public Map<String, Object> saveOrUpdateRole(RoleMenuTreeVoReqVo bmsRoleReqVo) throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		LogUtils.info(bmsRoleReqVo.toString());
		LogUtils.info(bmsRoleReqVo.getMenuTreeVoList());
		resultMap.put("result", bmsRoleService.saveAndUpdateRole(bmsRoleReqVo, getSessionUser()));
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("/addOrUpdateResourcet")
	public Map<String, Object> addOrUpdateResourcet(BmsResource bmsResource) throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		LogUtils.info(bmsResource.toString());
		resultMap.put("result", bmsRoleService.addOrUpdateResourcet(bmsResource, getSessionUser()));
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("/delResourcet")
	public Map<String, Object> delResourcet(Integer id) throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		LogUtils.info("id     :     "+ id);
		resultMap = bmsRoleService.delResourcet(id);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("/delRole")
	public Map<String, Object> delRole(Integer id) throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		LogUtils.info("id     :     "+ id);
		bmsRoleService.del(id);
		resultMap.put("result", true);
		return resultMap;
	}
}
